package cn.u313.lymusic.ui.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.u313.lymusic.activity.RoundAsyncImage
import cn.u313.lymusic.model.SearchModel.Data
import cn.u313.lymusic.utils.Height
import cn.u313.lymusic.utils.Width
import cn.u313.lymusic.viewmodel.MusicDownloadViewModel

class DownloadFromDialog(val mData: Data,val viewModel: MusicDownloadViewModel) : BaseDialog() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return ComposeView(requireContext()).apply {
            setContent {
                DownloadDialogView(mData){ mid,q->
                    viewModel.musicInfo.value = mData
                    viewModel.download(mid,q)
                }
            }
        }
    }
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val onCreateDialog = super.onCreateDialog(savedInstanceState)
        onCreateDialog.window?.apply {
            // 设置Dialog的宽度
            val layoutParams = WindowManager.LayoutParams()
            layoutParams.copyFrom(this.attributes)
            layoutParams.width = 300 // 设置宽度为300dp
            setAttributes(layoutParams)
        }
        return onCreateDialog
    }
}

//@Preview
@Composable
fun DownloadDialogView(mData:Data,click:(String,String)->Unit = {_,_->}) {
    Column(Modifier.width(300.dp)) {
        Height(int = 20)
        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
            Width(int = 20)
            RoundAsyncImage(
                modifier = Modifier
                    .size(40.dp),
                mData.pic
            )
            Width(int = 10)
            Column(Modifier) {
                Text(text = "${mData.song} - ${mData.singger}")
                Text(text = "专辑：${mData.album}", fontSize = 12.sp)
            }
        }
        Height(int = 20)
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            if (mData.size128 != 0L)
                DownloadItemView(mData.key, "128k", mData.size128) {
                    click.invoke(mData.key,"128k")
                }
            if (mData.size320 != 0L)
                DownloadItemView(mData.key,"320k",mData.size320){
                    click.invoke(mData.key,"320k")
                }
            if (mData.sizeape != 0L)
                DownloadItemView()
            if (mData.sizeflac != 0L)
                DownloadItemView(mData.key,"flac",mData.sizeflac){
                    click.invoke(mData.key,"flac")
                }
            Box(Modifier.height(50.dp), contentAlignment = Alignment.Center) {
                Text(text = "封面 / 歌词 暂不支持下载，后续迭代加上！",
                    color = Color(0x39070707),
                    fontSize = 10.sp
                )
            }
        }
    }
}

@Preview
@Composable
fun DownloadItemView(mid:String = "",str:String = "128kbs",size:Long = 0,click:()->Unit = {}) {
    Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
        .height(40.dp)
        .clickable {
            click.invoke()
        }) {
        Text(text = "点击下载 ", color = Color(0xFF2B2A2A))
        Text(text = str, color = Color(0xFFFF5722))
        Width(int = 10)
        Text(text = "( ${size*100/1024/1024/100.0} MB )", color = Color(0xFFFF5722))

    }
}